package com.xiaowan.interceptor;

import com.xiaowan.utils.threadlocal.ThreadLocalUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaowanmate
 * @date 2023/2/21 18:56
 */
//配置器（WebMvcConfigurer）实现
public class ServiceInterceptor implements HandlerInterceptor {

    //预处理，在业务处理器处理请求之前被调用，可以进行登录拦截，编码处理、安全控制、权限校验等处理；
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取用户id
        String userId = request.getHeader("userId");

        if(userId!=null && !userId.isEmpty()){
            ThreadLocalUtils.setUserId(Integer.parseInt(userId));
        }
        //给token续签
        if (request.getHeader("refreshToken") !=null){
            response.addHeader("refreshToken",request.getHeader("refreshToken"));
        }
        return true;
    }
}
